#include "sweepDialog.h"
#include "treeItem.h"
#include "faceTreeItem.h"
#include "src/face.h"

SweepDialog::SweepDialog(QList<QTreeWidgetItem *> _list, QWidget *_parent) : QDialog(_parent), list(_list){
    setWindowTitle(QString::fromUtf8("Sweep"));
    sweepX = sweepY = sweepZ = 0;

    QLabel *sweepXLabel = new QLabel(tr("X:"));

    QDoubleSpinBox *sweepXDoubleSpinBox = new QDoubleSpinBox();
    sweepXDoubleSpinBox->setRange(-50.0, 50.0);
    sweepXDoubleSpinBox->setSingleStep(1.0);
    sweepXDoubleSpinBox->setValue(sweepX);
    connect(sweepXDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeSweepX(double)));


    QLabel *sweepYLabel = new QLabel(tr("Y:"));

    QDoubleSpinBox *sweepYDoubleSpinBox = new QDoubleSpinBox();
    sweepYDoubleSpinBox->setRange(-50.0, 50.0);
    sweepYDoubleSpinBox->setSingleStep(1.0);
    sweepYDoubleSpinBox->setValue(sweepY);
    connect(sweepYDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeSweepY(double)));


    QLabel *sweepZLabel = new QLabel(tr("Z:"));

    QDoubleSpinBox *sweepZDoubleSpinBox = new QDoubleSpinBox();
    sweepZDoubleSpinBox->setRange(-50.0, 50.0);
    sweepZDoubleSpinBox->setSingleStep(1.0);
    sweepZDoubleSpinBox->setValue(sweepZ);
    connect(sweepZDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeSweepZ(double)));


    QHBoxLayout *sweepLayout = new QHBoxLayout;
    sweepLayout->addWidget(sweepXLabel);
    sweepLayout->addWidget(sweepXDoubleSpinBox);
    sweepLayout->addStretch(1);
    sweepLayout->addWidget(sweepYLabel);
    sweepLayout->addWidget(sweepYDoubleSpinBox);
    sweepLayout->addStretch(1);
    sweepLayout->addWidget(sweepZLabel);
    sweepLayout->addWidget(sweepZDoubleSpinBox);
    sweepLayout->addStretch(1);

    QGroupBox *sweepGroupBox = new QGroupBox(QString::fromUtf8("Vetor de sweep"));
    sweepGroupBox->setLayout(sweepLayout);


    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(acceptAndCreate()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    buttonBox->setLocale(QLocale(QLocale::Portuguese, QLocale::Brazil));

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(sweepGroupBox);
    mainLayout->addWidget(buttonBox);
    mainLayout->addStretch(1);

    setLayout(mainLayout);

}

SweepDialog::~SweepDialog(){
}

void SweepDialog::acceptAndCreate(){
    Vector3D sweepVector(sweepX, sweepY, sweepZ);

    QTreeWidgetItem *item = list.front();
    unsigned int type = ((TreeItem *)item)->getType();

    if(type == DEFAULT)
    {
        for(int i = 0; i < item->childCount(); i++ )
        {
            FaceTreeItem *itemChild = (FaceTreeItem *)item->child(i);
            Face *f = itemChild->getFace();
            f->getObject()->sweep(f, sweepVector);
        }
    }
    else
    {
        for(int i = 0; i < list.size(); i++){
            FaceTreeItem *itemChild = (FaceTreeItem *)list.at(i);
            Face *f = itemChild->getFace();
            f->getObject()->sweep(f, sweepVector);
        }
    }

    accept();
    done();
}

void SweepDialog::changeSweepX(double _x){
    sweepX = _x;
}

void SweepDialog::changeSweepY(double _y){
    sweepY = _y;
}

void SweepDialog::changeSweepZ(double _z){
    sweepZ = _z;
}
